/**
 * 解除对象与对象之间的紧耦合关系。增加一个中介者对象后，所有的 相关对象都通过中介者对象来通信，而不是互相引用，所以当一个对象发生改变时，只需要通知 中介者对象即可。中介者使各对象之间耦合松散，而且可以独立地改变它们之间的交互。中介者
模式使网状的多对多关系变成了相对简单的一对多关系（类似于观察者模式，但是单向的，由中介者统一管理。）
 */

class PlayerMiddle{
    players = [];
    winers = [];
    losers = [];

    add(name:string){
        this.players.push(name)
    }

    win(name:string){
        this.winers.push(name)
        this.show()
    }

    lose(name:string){
        this.losers.push(name)
        this.show()
    }
    show(){
        if(this.winers.length + this.losers.length == this.players.length){
            for(let name of this.winers){
                console.log(name+"--挑战成功",)
            }
            for(let name of this.losers){
                console.log(`${name} -- 挑战失败`)
            }
        }
    }
}

class Player {
    private name:string;
    private playMiddle:PlayerMiddle;
    constructor(playMiddle:PlayerMiddle, name:string){
        this.playMiddle = playMiddle;
        this.name = name
        this.playMiddle.add(name);
    }

    public win(){
        this.playMiddle.win(this.name)
    }
    public lose(){
        this.playMiddle.lose(this.name)
    }
}

const playMiddle = new PlayerMiddle();

let p1 = new Player(playMiddle, 'p1');
let p2 = new Player(playMiddle, 'p2');
let p3 = new Player(playMiddle, 'p3');

p1.win()
p2.win()
p3.lose()